package com.xxcxcxcx.jobsystem.mixin.job.Blacksmith;

import com.google.common.collect.Multimap;
import com.xxcxcxcx.jobsystem.Main;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.HashMap;

import static com.xxcxcxcx.jobsystem.common.Job.Blacksmith.Blacksmith.MakeTool;
import static com.xxcxcxcx.jobsystem.common.Job.Blacksmith.Blacksmith.ToolLevel;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {
    @Shadow public abstract CompoundTag getOrCreateTagElement(String p_41699_);

    @Shadow public abstract void removeTagKey(String p_41750_);
    String LastLevel="";

    private HashMap<EquipmentSlot,Multimap<Attribute, AttributeModifier>> hashMap= new HashMap<>();
    @Inject(method = "getAttributeModifiers", at = @At("TAIL"),cancellable = true)
    private void originsClasses$resetPlayerAndPos(EquipmentSlot p_41639_, CallbackInfoReturnable<Multimap<Attribute, AttributeModifier>> cir) {
        CompoundTag compoundTag=this.getOrCreateTagElement(Main.MOD_ID);
        if(compoundTag.isEmpty()){
            this.removeTagKey(Main.MOD_ID);
        }else{
            String s = compoundTag.getString(MakeTool+ToolLevel);
            if(!s.equals(LastLevel)){
                LastLevel=s;
                hashMap.clear();
            }
        }
        if(hashMap.containsKey(p_41639_)){
            cir.setReturnValue(hashMap.get(p_41639_));
        }
        else hashMap.put(p_41639_,cir.getReturnValue());
    }
}
